package com.sky.controller.user;

import com.sky.constant.DefaultAddressConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sky.service.AddressBookService;

import java.util.List;

@RestController
@RequestMapping(value = "/user/addressBook")
@Slf4j
@Api(tags = "用户地址管理相关接口")
public class AddressBookController {
    // 注入service对象
    @Autowired
    private AddressBookService addressBookService;

    /*查看所有地址方法*/
    @GetMapping(value = "/list")
    @ApiOperation(value = "条件查询地址方法")
    public Result<List<AddressBook>> list() {
        // 首先获取当前用户id
        Long userId = BaseContext.getCurrentId();
        log.info("查看用户的所有地址，当前用户的id{}", userId);
        // 新建一个AddressBook对象用于条件查询
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(userId);
        // 调用service层方法
        List<AddressBook> addressBookList = addressBookService.list(addressBook);
        return Result.success(addressBookList);
    }

    /*新增地址方法*/
    @PostMapping(value = "")
    @ApiOperation(value = "用户新增地址方法")
    public Result<String> save(@RequestBody AddressBook addressBook) {
        log.info("用户新增地址方法,新增地址参数为{}", addressBook);
        // 调用service层
        addressBookService.save(addressBook);
        return Result.success();
    }

    /*根据id修改地址信息，首先要数据回显，根据id查询地址*/
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据id查询地址信息方法")
    public Result<AddressBook> getAddressBookById(@PathVariable Long id) {
        log.info("根据id查询地址信息，id为{}", id);
        return Result.success(addressBookService.getAddressBookById(id));
    }

    /*修改地址信息*/
    @PutMapping(value = "")
    @ApiOperation(value = "修改地址信息方法")
    public Result<String> update(@RequestBody AddressBook addressBook) {
        log.info("修改地址信息，修改地址信息参数为{}", addressBook);
        // 调用service层
        addressBookService.update(addressBook);
        return Result.success();
    }

    /*将某一个非默认地址设置为默认地址*/
    @PutMapping(value = "/default")
    @ApiOperation(value = "将某地址设置为默认地址方法")
    public Result<String> setDefault(@RequestBody AddressBook addressBook) {
        log.info("将某地址设置为默认地址，参数为{}", addressBook);
        // 调用service层
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

    /*根据id删除地址*/
    @DeleteMapping(value = "")
    @ApiOperation(value = "根据id删除地址方法")
    public Result<String> deleteById(Long id) {
        log.info("根据id删除地址，id为{}", id);
        // 调用service层
        addressBookService.deleteById(id);
        return Result.success();
    }

    /*查看当前用户默认地址*/
    @GetMapping(value = "/default")
    @ApiOperation(value = "查看当前用户默认地址方法")
    public Result<AddressBook> getDefaultAddress() {
        // 设置查询条件
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(DefaultAddressConstant.DEFAULT);
        addressBook.setUserId(BaseContext.getCurrentId());
        // 调用service层list方法条件查询
        List<AddressBook> addressBookList = addressBookService.list(addressBook); // 根据userId 和 默认地址 条件查询
        if (addressBookList == null) {
            return Result.error("没有默认地址");
        }
        return Result.success(addressBookList.get(0));
    }
}
